package com.net355.eblog.util;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

public class CacheHelper {
	private static Log LOG = LogFactory.getLog(CacheHelper.class);
	private static CacheManager manager;
	private static Cache data_cache;
	private static Cache text_cache;
	private static Cache html_cache;

	public static void initEhCache(InputStream in) throws Exception {
		try {
			System.setProperty("net.sf.ehcache.enableShutdownHook", "true");
			if(in==null) {
				manager = CacheManager.create(System.getProperty("user.dir") + "/conf/ehcache_local.xml");
			} else {
				manager = CacheManager.create(in);
			}
			data_cache = manager.getCache("datacache");
			text_cache = manager.getCache("textcache");
			html_cache = manager.getCache("htmlcache");
			
			if(CacheHelper.get_cache(GlobalParams.CACHE_NAME_COUNT_SEARCH)==null) {
				CacheHelper.set_cache(GlobalParams.CACHE_NAME_COUNT_SEARCH, 0);
			}
			if(CacheHelper.get_cache(GlobalParams.CACHE_NAME_COUNT_CHAT)==null) {
				CacheHelper.set_cache(GlobalParams.CACHE_NAME_COUNT_CHAT, 0);
			}
			if(CacheHelper.get_cache(GlobalParams.CACHE_NAME_COUNT_VIEW)==null) {
				CacheHelper.set_cache(GlobalParams.CACHE_NAME_COUNT_VIEW, 0);
			}
			if(CacheHelper.get_cache(GlobalParams.CACHE_NAME_COUNT_UPDATE)==null) {
				CacheHelper.set_cache(GlobalParams.CACHE_NAME_COUNT_UPDATE, 0);
			}
			if(CacheHelper.get_cache(GlobalParams.CACHE_NAME_LIST_NOTEBOOK)==null) {
				CacheHelper.set_cache(GlobalParams.CACHE_NAME_LIST_NOTEBOOK, new ArrayList<String>());
			}
			if(CacheHelper.get_cache(GlobalParams.CACHE_NAME_LIST_NOTE)==null) {
				CacheHelper.set_cache(GlobalParams.CACHE_NAME_LIST_NOTE, new ArrayList<String>());
			}
			if(CacheHelper.get_cache(GlobalParams.CACHE_NAME_LIST_TAG)==null) {
				CacheHelper.set_cache(GlobalParams.CACHE_NAME_LIST_TAG, new ArrayList<String>());
			}
			if(CacheHelper.get_cache(GlobalParams.CACHE_NAME_LIST_CHAT_SYSTEM)==null) {
				CacheHelper.set_cache(GlobalParams.CACHE_NAME_LIST_CHAT_SYSTEM, new ArrayList<String>());
			}
			if(CacheHelper.get_cache(GlobalParams.CACHE_NAME_LIST_GAME_SCORE)==null) {
				CacheHelper.set_cache(GlobalParams.CACHE_NAME_LIST_GAME_SCORE, new ArrayList<String>());
			}
		} catch (Exception e) {
			LOG.error("initEhCache error--->>>", e);
			throw e;
		} finally {
			if(in!=null) {
				in.close();
			}
		}
	}

	/**
	 * add cache
	 * 
	 * @param cacheName
	 * @param obj
	 * @return
	 */
	public static boolean set_cache(String cacheName, Object obj) {
		try {
			Element element = new Element(cacheName, obj);
			data_cache.put(element);
			data_cache.flush();
			return true;
		} catch (Exception e) {
			LOG.error("set_cache error with cache " + cacheName + "--->>>", e);
			return false;
		}
	}
	public static boolean set_cache(String cacheName, Object obj, int idelTime, int liveTime) {
		try {
			Element element = new Element(cacheName, obj, idelTime, liveTime);
			data_cache.put(element);
			data_cache.flush();
			return true;
		} catch (Exception e) {
			LOG.error("set_cache error with cache " + cacheName + "--->>>", e);
			return false;
		}
	}

	public static boolean set_cache_text(String cacheName, Object obj) {
		try {
			Element element = new Element(cacheName, obj);
			text_cache.put(element);
			text_cache.flush();
			return true;
		} catch (Exception e) {
			LOG.error("set_cache_text error with cache " + cacheName + "--->>>", e);
			return false;
		}
	}

	public static boolean set_cache_html(String cacheName, Object obj) {
		try {
			Element element = new Element(cacheName, obj);
			html_cache.put(element);
			html_cache.flush();
			return true;
		} catch (Exception e) {
			LOG.error("set_cache_html error with cache " + cacheName + "--->>>", e);
			return false;
		}
	}

	/**
	 * 读取缓存
	 * 
	 * @param cacheName
	 * @return
	 */
	public static Object get_cache(String cacheName) {
		try {
			Element element = data_cache.get(cacheName);
			if (element != null)
				return element.getObjectValue();
		} catch (Exception e) {
			LOG.error("get_cache error--->>>", e);
		} finally {
			data_cache.flush();
		}
		return null;
	}

	public static Object get_cache_text(String cacheName) {
		try {
			Element element = text_cache.get(cacheName);
			if (element != null)
				return element.getObjectValue();
		} catch (Exception e) {
			LOG.error("get_cache_text error--->>>", e);
		} finally {
			text_cache.flush();
		}
		return null;
	}

	public static Object get_cache_html(String cacheName) {
		try {
			Element element = html_cache.get(cacheName);
			if (element != null)
				return element.getObjectValue();
		} catch (Exception e) {
			LOG.error("get_cache_html error--->>>", e);
		} finally {
			html_cache.flush();
		}
		return null;
	}

	/**
	 * 移除缓存
	 * 
	 * @param cacheName
	 * @return
	 */
	public static boolean remove_cache(String cacheName) {
		try {
			return data_cache.remove(cacheName);
		} catch (Exception e) {
			LOG.error("remove_cache error--->>>", e);
			return false;
		} finally {
			data_cache.flush();
		}
	}

	public static boolean remove_cache_text(String cacheName) {
		try {
			return text_cache.remove(cacheName);
		} catch (Exception e) {
			LOG.error("remove_cache_text error--->>>", e);
			return false;
		} finally {
			text_cache.flush();
		}
	}

	public static boolean remove_cache_html(String cacheName) {
		try {
			return html_cache.remove(cacheName);
		} catch (Exception e) {
			LOG.error("remove_cache_html error--->>>", e);
			return false;
		} finally {
			html_cache.flush();
		}
	}

	/**
	 * 移除所有缓存
	 */
	public static void remove_all_cache() {
		try {
			data_cache.removeAll();
		} catch (Exception e) {
			LOG.error("remove_all_cache error--->>>", e);
		}
	}

	public static void remove_all_cache_text() {
		try {
			text_cache.removeAll();
		} catch (Exception e) {
			LOG.error("remove_all_cache_text error--->>>", e);
		}
	}

	public static void remove_all_cache_html() {
		try {
			html_cache.removeAll();
		} catch (Exception e) {
			LOG.error("remove_all_cache_html error--->>>", e);
		}
	}

	/**
	 * 获取缓存信息
	 * 
	 * @return
	 */
	public static List<String> get_cache_info() {
		List<String> itemList = new ArrayList<String>();
		try {
			// itemList.add(data_cache.getKeys().toString());
			// //得到缓存对象key列表(List)
			// itemList.add("缓存中缓存对象的数量-->"+data_cache.getSize());
			// //得到缓存中缓存对象的数量
			// itemList.add("缓存对象占用内存的大小-->"+(data_cache.getStatistics().getSize()));
			// //得到缓存对象占用内存的大小
			itemList.add("data_cache :::: "+data_cache.getSize()+"_disk["+data_cache.getStatistics().getLocalDiskSize()+"]["+data_cache.getStatistics().getLocalDiskSizeInBytes()/1024+"K]" +
					"_heap["+data_cache.getStatistics().getLocalHeapSize()+"]["+data_cache.getStatistics().getLocalHeapSizeInBytes()/1024+"]");
			itemList.add("html_cache :::: "+html_cache.getSize()+"_disk["+html_cache.getStatistics().getLocalDiskSize()+"]["+html_cache.getStatistics().getLocalDiskSizeInBytes()/1024+"K]" +
					"_heap["+html_cache.getStatistics().getLocalHeapSize()+"]["+html_cache.getStatistics().getLocalHeapSizeInBytes()/1024+"]");
			itemList.add("text_cache :::: "+text_cache.getSize()+"_disk["+text_cache.getStatistics().getLocalDiskSize()+"]["+text_cache.getStatistics().getLocalDiskSizeInBytes()/1024+"K]" +
					"_heap["+text_cache.getStatistics().getLocalHeapSize()+"]["+text_cache.getStatistics().getLocalHeapSizeInBytes()/1024+"]");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return itemList;
	}

	public static void main(String[] args) {
		try {
			// System.out.println("result-->"+System.getProperty("user.dir"));
			System.setProperty("net.sf.ehcache.enableShutdownHook", "true");
			manager = CacheManager.create(System.getProperty("user.dir") + "/conf/ehcache_local.xml");
			data_cache = manager.getCache("datacache");
			text_cache = manager.getCache("textcache");
			html_cache = manager.getCache("htmlcache");
			System.out.println("ray_print-sign-->" + CacheHelper.get_cache(GlobalParams.CACHE_NAME_COUNT_UPDATE));
			System.out.println("data_cache :::: "+data_cache.getSize()+"_disk["+data_cache.getStatistics().getLocalDiskSize()+"]["+data_cache.getStatistics().getLocalDiskSizeInBytes()/1024+"K]" +
					"_heap["+data_cache.getStatistics().getLocalHeapSize()+"]["+data_cache.getStatistics().getLocalHeapSizeInBytes()/1024+"]");
			List<?> items = data_cache.getKeys();
			for (int i = 0; i < items.size(); i++) {
				System.out.println(items.get(i)+"-->"+CacheHelper.get_cache(items.get(i)+""));
			}
			
			// for (int i = 100; i < 200; i++) {
			// set_cache("test_"+i, "value-"+i);
			// }
			//
			// for (int i = 100; i < 200; i++) {
			// System.out.println("ray_print-->"+get_cache("test_"+i));
			// }
			//
			// System.out.println("ray_print-getSize-->"+data_cache.getSize());
			// System.out.println("ray_print-getLocalDiskSize-->"+data_cache.getStatistics().getLocalDiskSize());
			// System.out.println("ray_print-getLocalDiskSizeInBytes-->"+data_cache.getStatistics().getLocalDiskSizeInBytes());
			// System.out.println("ray_print-getLocalHeapSize-->"+data_cache.getStatistics().getLocalHeapSize());
			// System.out.println("ray_print-getLocalHeapSizeInBytes-->"+data_cache.getStatistics().getLocalHeapSizeInBytes());
			// data_cache.dispose();
			// data_cache.setDisabled(true);
			// System.out.println("ray_print-data_cache-->"+data_cache);
			// System.out.println("ray_print-->"+get_cache("test_2"));
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			System.exit(0);
		}
	}
}
